package com.yunwushanhe.document.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*拦截器类*/
@Slf4j
public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //登陆成功后，应该有用户的session
        String requestURI = request.getRequestURI();
        log.info("拦截的请求路径有{}",requestURI);
        Object loginUser = request.getSession().getAttribute("loginUser");
        if(loginUser == null){
            request.setAttribute("msg1","没有权限，请先登录！");
            request.getRequestDispatcher("/login.html").forward(request,response);//被拦截后，请求转发到登录页面
            return false;
        } else {
            return true;
        }
    }
}
